program Multiplication;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  pelement = ^element;
  element = record
    v1, v2: Integer;
    next: pelement;
  end;
procedure PrintList (head: pelement);
var
  current: pelement;
begin
  current := head;
  while (current <> nil) do
  begin
    Writeln (current^.v1, ' ', current^.v2);
    current := current^.next;
  end;
end;


procedure AddToBegin (var head: pelement; num1, num2: integer);
var
  tmp : pelement;
begin
  tmp := head;
  new(head);
  head^.next := tmp;
  head^.v1 := num1;
  head^.v2 := num2;
end;

procedure AddToEnd (var head: pelement; num1, num2: integer);
var
  current: pelement;
begin
  if head <> nil then
  begin
    current := head;
    while (current^.next <> nil) do
    begin
      current := current^.next;
    end;
    new (current^.next);
    current^.next^.v1 := num1;
    current^.next^.v2 := num2;
    current^.next^.next := nil;
  end else begin
    AddToBegin(head, num1, num2);
  end;
end;


var
  fin: TextFile;
  n, m, i, j, a, b, c, d, e, v1: Integer;
  tmp1, tmp2, tmp3, current1, current2, current3, head1, head2, head3: pelement;

begin
  AssignFile(fin, 'inputmult.txt');
  Reset(fin);
  head1 := nil;
  head2 := nil;
  head3 := nil;
  read(fin, n);
  for i := 1 to n do
  begin
    read (fin, a);
    read (fin, v1);
    AddToEnd(head1, a, v1);
  end;
  read(fin, m);
  for i := 1 to m do
  begin
    read (fin, b);
    read (fin, e);
    AddToEnd(head2, b, e);
  end;
  current1 := head1;
  current2 := head2;
  current1 := head1;
  current2 := head2;
  if n <= m then
  begin
    for i := 1 to n do
    begin
      for j := 1 to m do
      begin
        c := current1^.v1*current2^.v1;
        d := current1^.v2+current2^.v2;
        AddToEnd(head3, c, d);
        current2 := current2^.next;
      end;
      current1 := current1^.next;
      current2 := head2;
    end;
  end else begin
    for i := 1 to m do
    begin
      for j := 1 to n do
      begin
        c := current1^.v1*current2^.v1;
        d := current1^.v2 + current2^.v2;
        AddToEnd(head3, c, d);
        current1 := current1^.next;
      end;
      current2 := current2^.next;
      current1 := head1;
    end;
  end;
  current3 := head3;
  while current3 <> nil do
  begin
    tmp1 := current3^.next;
    tmp2 := current3;
    while tmp1 <> nil do
    begin
      if tmp1^.v2 = current3^.v2 then
      begin
        current3^.v1 := current3^.v1 + tmp1^.v1;
        tmp2^.next := tmp1^.next;
        Dispose (tmp1);
        tmp1 := tmp2^.next;
      end else begin
        tmp1 := tmp1^.next;
        tmp2 := tmp2^.next;
      end;
    end;
    current3 := current3^.next;
  end;
  current3 := head3^.next;
  tmp2 := head3;
  while current3 <> nil do
  begin
    tmp1 := current3^.next;
    tmp3 := current3;
    while tmp1 <> nil do
    begin
      if tmp1^.v2 > current3^.v2 then
      begin
        New(tmp2^.next);
        tmp2^.next^.v1 := tmp1^.v1;
        tmp2^.next^.v2 := tmp1^.v2;
        tmp2^.next^.next := current3;
        tmp3^.next := tmp1^.next;
        Dispose(tmp1);
        tmp1 := tmp3^.next;
        tmp2 := tmp2^.next;
      end else begin
        tmp1 := tmp1^.next;
        tmp3 := tmp3^.next;
      end;
    end;
    tmp2 := current3;
    current3 := current3^.next;
  end;
  PrintList(head3);
  CloseFile(fin);
  readln;
end.
